//
// Created by Abstergo on 2024/2/11.
//

#ifndef QUICKGUARD_CUH
#define QUICKGUARD_CUH

namespace tbs {

template<typename T> void delete_ptr_method(T *ptr) {
  delete ptr;
}

template<typename T> void delete_array_ptr_method(T *ptr) {
  delete[]ptr;
}

template<typename V, typename W, typename... ARGS> WrappedGuard<V> make_wrapper_guard(ARGS... args) {
  static_assert(std::is_base_of<Wrapper<V>, W>::value, "W type must be wrapper");
  return WrappedGuard<V>(new W(args...));
}

template<typename V> WrappedGuard<V> make_function_guard(V &ptr, std::function<void(V &)> releaseAction) {
  return WrappedGuard<V>(new FunctionWrapper<V>(ptr, [releaseAction](FunctionWrapper<V> &p) {
	if (releaseAction != nullptr) {
	  releaseAction(p.value());
	}
  }));
}

template<typename V> ArrayPtrGuard<V> make_array_ptr_guard(V *ptr) {
  return ArrayPtrGuard<V>(ptr);
}

template<typename V> PtrGuard<V> make_ptr_guard(V *ptr) {
  return PtrGuard<V>(ptr);
}

}
#endif //QUICKGUARD_CUH
